/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package camada.apresentacao.dialog.mysql;

import arvore.tabelas.TreeModel;
import camada.bancodedados.daoimplemente.mysql.MysqlDAOImpl;
import eloma.classe.mysql.Campos;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.logging.Level;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import org.jdesktop.swingx.error.ErrorInfo;
import tabelamodelcolumnmodel.mysql.MysqlCamposColumnModel;
import tabelamodelcolumnmodel.mysql.MysqlCamposTableModel;
import tabelamodelcolumnmodel.mysql.MysqlTabelasColumnModel;
import tabelamodelcolumnmodel.mysql.MysqlTabelasTableModel;
import utilitarios.erros.MessagemErro;

/**
 *
 * @author KLINGER
 */
public class InternalFrameMysqlTabelas extends javax.swing.JInternalFrame {

    int indexTabela = 0;
    MysqlDAOImpl daoMy = new MysqlDAOImpl();

    /**
     * Creates new form InternalFrameMysqlTabelas
     */
    public InternalFrameMysqlTabelas() {
        initComponents();
        carregarTabela();
    }

    public void carregarTabela() {

        try {

            CustomTreeExample();
            List<String> listaTabelas = daoMy.getTabelasBanco();

            tbTabelas.setModel(new MysqlTabelasTableModel(listaTabelas));

            tbTabelas.setAutoCreateColumnsFromModel(false);
            java.awt.FontMetrics fm = tbTabelas.getFontMetrics(tbTabelas.getFont());

            //Aki tem um exemplo de como colocar a MyCenderSelll.
            tbTabelas.setColumnModel(new MysqlTabelasColumnModel(fm));
            tbTabelas.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        } catch (Exception ex) {
        }

    }

    // Constructor of main frame
    public void CustomTreeExample() throws Exception {

//        // Create data for the tree
//        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Eloma");
//
//        List<String> listaTabelas = daoMy.getTabelasBanco();
//        
//        for (String tabela : listaTabelas) {
//            CreateSuit(root, tabela);
//        }
//        
//        TreeModel treeModel = new TreeModel(root);
//        jTree1.setModel(treeModel);
    }

    public void CreateSuit(DefaultMutableTreeNode root, String suitName) {
        try {

            List<Campos> lista = daoMy.getCamposTabela(suitName);

            DefaultMutableTreeNode itemSuit = new DefaultMutableTreeNode(suitName);

            for (Campos campos : lista) {
                itemSuit.add(new DefaultMutableTreeNode(campos.getField()));
            }

            root.add(itemSuit);
        } catch (Exception ex) {
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenu1 = new javax.swing.JMenu();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbCampos = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtArea = new javax.swing.JTextArea();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbTabelas = new javax.swing.JTable();
        jXHyperlink1 = new org.jdesktop.swingx.JXHyperlink();

        jMenu1.setText("jMenu1");

        setClosable(true);
        setIconifiable(true);
        setTitle("Mysql Tabelas Banco de Dados");
        setToolTipText("");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/sistema/Icone_Sistema.png"))); // NOI18N

        jTabbedPane1.setToolTipText("");

        tbCampos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tbCampos.setAutoscrolls(false);
        tbCampos.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbCamposKeyPressed(evt);
            }
        });
        jScrollPane2.setViewportView(tbCampos);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 705, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 451, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Campos", jPanel1);

        txtArea.setColumns(20);
        txtArea.setRows(5);
        jScrollPane3.setViewportView(txtArea);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 705, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 451, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Script", jPanel2);

        tbTabelas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tbTabelas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbTabelasMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbTabelas);

        jXHyperlink1.setText("jXHyperlink1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jXHyperlink1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 730, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jXHyperlink1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tbTabelasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbTabelasMouseClicked
        try {

            indexTabela = tbTabelas.getSelectedRow();

            MysqlTabelasTableModel model = (MysqlTabelasTableModel) tbTabelas.getModel();
            String s = model.getValoresString(indexTabela);

            List<Campos> listaCampos = daoMy.getCamposTabela(s);

            tbCampos.setModel(new MysqlCamposTableModel(listaCampos));

            tbCampos.setAutoCreateColumnsFromModel(false);
            java.awt.FontMetrics fm = tbCampos.getFontMetrics(tbCampos.getFont());

            //Aki tem um exemplo de como colocar a MyCenderSelll.
            tbCampos.setColumnModel(new MysqlCamposColumnModel(fm));
            tbCampos.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

            txtArea.setText(daoMy.getScriptTabela(s));

        } catch (Exception e) {
            MessagemErro erro = new MessagemErro(new ErrorInfo("Erro no Sistema", "Não é Possível Continuar, Sistema Inativo", null, "Fatal", e, Level.WARNING, null), 1);
        }
    }//GEN-LAST:event_tbTabelasMouseClicked

    private void tbCamposKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbCamposKeyPressed
        if (evt.getKeyCode() == 17) {

            ((MysqlTabelasTableModel) tbCampos.getModel()).removeRow(tbCampos.getSelectedRow());

        }
    }//GEN-LAST:event_tbCamposKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private org.jdesktop.swingx.JXHyperlink jXHyperlink1;
    private javax.swing.JTable tbCampos;
    private javax.swing.JTable tbTabelas;
    private javax.swing.JTextArea txtArea;
    // End of variables declaration//GEN-END:variables
}
